package com.example.bus.demos.web.controller;

import com.example.bus.demos.web.entity.BusRoute;
import com.example.bus.demos.web.service.BusRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/bus-routes")
public class BusRouteController {

    @Autowired
    private BusRouteService busRouteService;

    @GetMapping
    public List<BusRoute> getAllRoutes() {
        return busRouteService.getAllRoutes();
    }

    @GetMapping("/{id}")
    public ResponseEntity<BusRoute> getRouteById(@PathVariable Long id) {
        BusRoute busRoute = busRouteService.getRouteById(id);
        return busRoute != null ? ResponseEntity.ok(busRoute) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<Void> addRoute(@RequestBody BusRoute busRoute) {
        busRouteService.addRoute(busRoute);
        return ResponseEntity.created(null).build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateRoute(@PathVariable Long id, @RequestBody BusRoute busRoute) {
        busRoute.setId(id);
        busRouteService.updateRoute(busRoute);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRoute(@PathVariable Long id) {
        busRouteService.deleteRoute(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/search/name/{routeName}")
    public List<BusRoute> searchByRouteName(@PathVariable String routeName) {
        return busRouteService.searchByRouteName(routeName);
    }

    @GetMapping("/search/number/{routeNumber}")
    public List<BusRoute> searchByRouteNumber(@PathVariable String routeNumber) {
        return busRouteService.searchByRouteNumber(routeNumber);
    }
}